﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shCore.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushCSharp.js"></script>
    <script type="text/javascript" src="../../Content/highlighter/scripts/shBrushXml.js"></script>
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCore.css" />
    <link type="text/css" rel="stylesheet" href="../../Content/highlighter/styles/shCoreEclipse.css" />
    <script type="text/javascript">SyntaxHighlighter.all();</script>
</head>
<body>
    <h3>比微软Dictionary快30%的FastDictionary类</h3>
    <p>
        该类经过优化后比传统字典类快，使用方式也一样，没有区别。
        这里重点说明一下错误用法：
        <pre class="brush:csharp;">
            using NFinal;
            public class IndexController : BaseController
            {
                public static FastDictionary&lt;string,string> dictionary=null;
                public void Index()
                {
                    if(dictionary==null)
                    {
                        dictionary=new FastDictionary&lt;string,string>();
                        dictionary.Add("网站域名","http://bbs.nfinal.com");
                        dictionary.Add("网站名称","NFinal交流论坛");
                    }
                }
            }
        </pre>
        <b style="color:red;">以上用法是完全错误的！因为Index是多线程运行，dictionary会多次初始化。<br/>
        必须使用System.Collections.Concurrent.ConcurrentDictionary&lt;string,string>类型</b>
        <pre class="brush:csharp;">
            using NFinal;
            using System.Collections.Concurrent;
            public class IndexController : BaseController
            {
                public static ConcurrentDictionary&lt;string,string> dictionary=null;
                public void Index()
                {
                    if(dictionary==null)
                    {
                        dictionary=new ConcurrentDictionary&lt;string,string>();
                        dictionary.Add("网站域名","http://bbs.nfinal.com");
                        dictionary.Add("网站名称","NFinal交流论坛");
                    }
                }
            }
        </pre>
        <b style="color:red">以上用法也是完全错误的！因为ConcurrentDictionary并没有对初始化作多线程处理<br/>
            因此该类型的初始化必须在Startup类中初始化，或者直接使用静态字段声明初始化。例：
        </b>
        <pre class="brush:csharp;">
            using NFinal;
            using System.Collections.Concurrent;
            public class IndexController : BaseController
            {
                //正确用法。
                public static ConcurrentDictionary&lt;string,string> dictionary=new ConcurrentDictionary&lt;string,string>();
                public void Index()
                {
                    dictionary.Add("网站域名","http://bbs.nfinal.com");
                    dictionary.Add("网站名称","NFinal交流论坛");
                }
            }
        </pre>
    </p>
</body>
</html>